<template>
	<view class="container">
		<custom-navi input :rigthAddres="locationValue" bgColor="#f8f8f8" @chooseCity="chooseCity"></custom-navi>
		<view class="content"></view>
		<ss-select-city v-if="ischooseCity" :hotCitys="hotCitys" v-model="locationValue" :currentCity="currentCity" :locationValue="locationValue" @chooseCity="chooseCity"/>
	</view>
	
</template>

<script>
	import ssSelectCity from '@/components/ss-select-city/ss-select-city.vue'
	export default{
		components: {
			ssSelectCity
		},
		data() {
			return{
				ischooseCity: false, //选择城市
				hotCitys: ['杭州', '天津', '北京', '上海', '深圳', '广州', '成都', '重庆', '厦门'],
				locationValue: '正在定位...', //显示的城市
				currentCity: '南宁', //当前城市
				
			}
		},
		onLoad(options) {
			this.getLocation()
		},
		methods: {
			//选择城市
			chooseCity(e) {
				console.log(e)
				this.locationValue = e.city
				this.ischooseCity = e.type
			},
			// 定位当前位置
			getLocation: function() {
				let that = this
				uni.getLocation({
					type: 'wgs84',
					geocode: true,
					success: function(res) {
							// #ifdef APP-PLUS
							var point = new plus.maps.Point(res.longitude, res.latitude);
							plus.maps.Map.reverseGeocode(
								point,
								{},
								function(event) {
									var address = event.address; // 转换后的地理位置
									var point = event.coord; // 转换后的坐标信息
									var coordType = event.coordType; // 转换后的坐标系类型
									console.log(address, 'address');
									var reg = /.+?(省|市|自治区|自治州|县|区)/g;
									console.log(address.match(reg));
									let addressList=address.match(reg).toString().split(",");
									let cityTitle= addressList[1];
									if (cityTitle.length > 0) {
										let lastChares = cityTitle.substring(cityTitle.length - 1);
										if (lastChares == "市") {
											cityTitle = cityTitle.substring(0, cityTitle.length - 1);
										}
										that.currentCity = that.locationValue = cityTitle
										// 全局定义触发的事件
										uni.$emit('upadteLoaction', cityTitle)
									}
								},
								function(e) {}
							);
							// #endif
					},
					fail: (err) => {
						console.log(err);
					}
				});
			},
			
		
		}
	}
</script>

<style>
	
</style>
